//
//  CleanCheckViewController.m
//  随便点
//
//  Created by ZC on 15/4/1.
//  Copyright (c) 2015年 qfsoft. All rights reserved.
//

#import "CleanCheckViewController.h"
#import "twoCheckView.h"
#import "DropDown2.h"
#import "AppDelegate.h"
#import "psztViewController.h"
#import "balancePaymentViewController.h"
#import "orderViewController.h"
#define Ktjdd @"http://www.51anys.com:8080/waimaiwang/order/confirmClean"

@interface CleanCheckViewController (){
    twoCheckView *twoView;
    int Y;
    AppDelegate *del;
    NSString *cleanNumber;
    NSString *moren;
    NSString *zffs;
    NSDictionary *sbxtjdic;
    UIAlertView *_alert;
}

@end

@implementation CleanCheckViewController

- (void)viewDidLoad {
    self.navigationItem.hidesBackButton = YES;
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed: @"back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = item;
    
    del=(AppDelegate *)[[UIApplication sharedApplication]delegate];
    //加载自定义的scrollview
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"twoCheckView" owner:self options:nil];
    twoView = nib[0];
    
    //框架scrollview的大小
    self.cleanCheckScrollView.contentSize = CGSizeMake(self.view.frame.size.width, twoView.frame.size.height);
    
    //把自定义的scrollview添加到框架scrollview上
    [self.cleanCheckScrollView addSubview:twoView];
    
    //起始高度
    Y = 10;
    
    // 加载第一个view的信息
    [self load];
    
    //加载第二个view的信息
    [self loadSecondView];
    
    [twoView.sbxtj addTarget:self action:@selector(actiontj) forControlEvents:UIControlEventTouchUpInside];
    
    [twoView.sbxqx addTarget:self action:@selector(actionqx) forControlEvents:UIControlEventTouchUpInside];
    
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
-(void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
    
    del.detailsVC.array=[NSMutableArray array];
    
    del.ViewC.ButtonChoose = 4;
    
}
//第一个view的信息
-(void)load{
    
    cleanNumber=del.cleanCount;
        [twoView SetupName:@"洗车券（测试）" Price:@"20.00" Number:cleanNumber SetY:Y];

}
//第二个view的信息
-(void)loadSecondView{
    DropDown2 *dd1 = [[DropDown2 alloc] initWithFrame:CGRectMake(10, 190, 355, 30)];
    NSArray *arr=[[NSArray alloc]initWithObjects:@"货到付款",@"余额付款",@"支付宝付款",@"建设网银支付",nil];
    dd1.tableArray = arr;
    dd1.textField.text =[arr objectAtIndex:0];
    moren = dd1.textField.text;
    [twoView.SecondView addSubview:dd1];
    
    twoView.totalCount.text=del.cleanCount;         //洗车券的总次数
    twoView.totalPrice.text=del.cleanPrice;        //洗车券的总金额
}
-(void)actiontj{
    if (del.anniu == nil||[del.anniu isEqual:@""]) {
        zffs = moren;
    }else{
        zffs = del.anniu;
    }
    NSString *dingdid;
    if (del.querId == nil||[del.querId isEqualToString:@""]) {
        dingdid = del.xhlddid;
    }else{
        dingdid = del.querId;
    }
    NSURL *url=[NSURL URLWithString:Ktjdd];
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
    NSString *postStr=[NSString stringWithFormat:@"orderId=%@&name=%@&phone=%@&mark=%@&payType=%@",dingdid,twoView.lablxr.text,twoView.lablxdh.text,twoView.textbeiz.text,zffs];
    [request setHTTPMethod:@"post"];
    NSData *data=[postStr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError){
        sbxtjdic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        [self performSelectorOnMainThread:@selector(refreshData) withObject:sbxtjdic waitUntilDone:YES];
        if ([zffs isEqual:@"货到付款"]) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"提交成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            _alert = alert;
            [alert show];
        }else if([zffs isEqual:@"建设网银支付"]){
            del.pd = @"建设网银";
            psztViewController *zt = [self.storyboard instantiateViewControllerWithIdentifier:@"psztVC"];
            [self.navigationController pushViewController:zt animated:YES];
        }else if ([zffs isEqual:@"支付宝付款"]){
            del.pd = @"支付宝";
            psztViewController *zt = [self.storyboard instantiateViewControllerWithIdentifier:@"psztVC"];
            [self.navigationController pushViewController:zt animated:YES];
        }else{
            balancePaymentViewController *bp = [self.storyboard instantiateViewControllerWithIdentifier:@"balancePaymentVC"];
            [self.navigationController pushViewController:bp animated:YES];
        }
    }];

}
-(void)refreshData{
    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==0&&alertView==_alert) {
        orderViewController *order = [self.storyboard instantiateViewControllerWithIdentifier:@"orderviewCV"];
        [self.navigationController pushViewController:order animated:YES];
    }
}
-(void)actionqx{
    [self.navigationController popViewControllerAnimated:YES];
    del.detailsVC.array=[NSMutableArray array];
    del.ViewC.ButtonChoose = 4;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
